package com.shardingsphere.shardingsphere.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shardingsphere.shardingsphere.annotation.AccessLimit;
import com.shardingsphere.shardingsphere.model.OrderInfo;
import com.shardingsphere.shardingsphere.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "用户分库分表")
@RestController
@RequestMapping("/api/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @ApiOperation("新增")
    @PostMapping("/save")
    public boolean save(@RequestBody OrderInfo orderInfo) {
        return orderService.save(orderInfo);
    }

    @ApiOperation("删除")
    @DeleteMapping("/{orderId}")
    public boolean deleteById(
            @PathVariable("orderId")
            Long id) {
        return orderService.removeById(id);
    }

    @ApiOperation("更新")
    @PutMapping("/{orderId}")
    public boolean updateById(
            @PathVariable("orderId")
            Long orderId,
            @RequestBody OrderInfo orderInfo) {
        orderInfo.setOrderId(orderId);
        return orderService.updateById(orderInfo);
    }

    @ApiOperation("查询")
    @GetMapping("/list")
    @AccessLimit
    public IPage page(
            @RequestParam(name = "pageNum", required = false, defaultValue = "1")
            Integer pageNum,
            @RequestParam(name = "pageSize", required = false, defaultValue = "10")
            Integer pageSize,
            @RequestParam(name = "orderId", required = false)
            Long orderId,
            @RequestParam(name = "orderStatus", required = false)
            Integer orderStatus,
            @RequestParam(name = "orderName", required = false)
            String orderName) {
        Page<OrderInfo> page = new Page<>(pageNum, pageSize);
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderId(orderId).setOrderName(orderName).setOrderStatus(orderStatus);
        return orderService.pageOrderInfos(page, orderInfo);
    }
}

